package com.ruoyi.book.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.book.domain.BookCommissionUsers;
import com.ruoyi.book.mapper.BookInstitutionInfoMapper;
import com.ruoyi.book.domain.BookInstitutionInfo;
import com.ruoyi.book.service.IBookInstitutionInfoService;

/**
 * 机构信息Service业务层处理
 * 
 * @author hd
 * @date 2025-03-31
 */
@Service
public class BookInstitutionInfoServiceImpl implements IBookInstitutionInfoService 
{
    @Autowired(required=false)
    private BookInstitutionInfoMapper bookInstitutionInfoMapper;

    /**
     * 查询机构信息
     * 
     * @param institutionId 机构信息主键
     * @return 机构信息
     */
    @Override
    public BookInstitutionInfo selectBookInstitutionInfoByInstitutionId(Long institutionId)
    {
        return bookInstitutionInfoMapper.selectBookInstitutionInfoByInstitutionId(institutionId);
    }

    /**
     * 查询机构信息列表
     * 
     * @param bookInstitutionInfo 机构信息
     * @return 机构信息
     */
    @Override
    public List<BookInstitutionInfo> selectBookInstitutionInfoList(BookInstitutionInfo bookInstitutionInfo)
    {
        return bookInstitutionInfoMapper.selectBookInstitutionInfoList(bookInstitutionInfo);
    }

    /**
     * 新增机构信息
     * 
     * @param bookInstitutionInfo 机构信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookInstitutionInfo(BookInstitutionInfo bookInstitutionInfo)
    {
        int rows = bookInstitutionInfoMapper.insertBookInstitutionInfo(bookInstitutionInfo);
        insertBookCommissionUsers(bookInstitutionInfo);
        return rows;
    }

    /**
     * 修改机构信息
     * 
     * @param bookInstitutionInfo 机构信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookInstitutionInfo(BookInstitutionInfo bookInstitutionInfo)
    {
        bookInstitutionInfoMapper.deleteBookCommissionUsersByInstitutionId(bookInstitutionInfo.getInstitutionId());
        insertBookCommissionUsers(bookInstitutionInfo);
        return bookInstitutionInfoMapper.updateBookInstitutionInfo(bookInstitutionInfo);
    }

    /**
     * 批量删除机构信息
     * 
     * @param institutionIds 需要删除的机构信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInstitutionInfoByInstitutionIds(Long[] institutionIds)
    {
        bookInstitutionInfoMapper.deleteBookCommissionUsersByInstitutionIds(institutionIds);
        return bookInstitutionInfoMapper.deleteBookInstitutionInfoByInstitutionIds(institutionIds);
    }

    /**
     * 删除机构信息信息
     * 
     * @param institutionId 机构信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInstitutionInfoByInstitutionId(Long institutionId)
    {
        bookInstitutionInfoMapper.deleteBookCommissionUsersByInstitutionId(institutionId);
        return bookInstitutionInfoMapper.deleteBookInstitutionInfoByInstitutionId(institutionId);
    }

    /**
     * 新增图书委托用户信息
     * 
     * @param bookInstitutionInfo 机构信息对象
     */
    public void insertBookCommissionUsers(BookInstitutionInfo bookInstitutionInfo)
    {
        List<BookCommissionUsers> bookCommissionUsersList = bookInstitutionInfo.getBookCommissionUsersList();
        Long institutionId = bookInstitutionInfo.getInstitutionId();
        if (StringUtils.isNotNull(bookCommissionUsersList))
        {
            List<BookCommissionUsers> list = new ArrayList<BookCommissionUsers>();
            for (BookCommissionUsers bookCommissionUsers : bookCommissionUsersList)
            {
                bookCommissionUsers.setInstitutionId(institutionId);
                list.add(bookCommissionUsers);
            }
            if (list.size() > 0)
            {
                bookInstitutionInfoMapper.batchBookCommissionUsers(list);
            }
        }
    }
}
